package com.ruoyi.car.controller;


import com.ruoyi.car.domain.CarList;
import com.ruoyi.car.service.ICarListService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 汽车Controller
 *
 * @author ruoyi
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/car/list")
public class CarListController extends BaseController {
    @Autowired
    private ICarListService carListService;

    /**
     * 查询汽车列表
     */
    @PreAuthorize("@ss.hasPermi('car:list:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarList carList) {
        startPage();
        List<CarList> list = carListService.selectCarListList(carList);
        return getDataTable(list);
    }

    /**
     * 导出汽车列表
     */
    @PreAuthorize("@ss.hasPermi('car:list:export')")
    @Log(title = "汽车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarList carList) {
        List<CarList> list = carListService.selectCarListList(carList);
        ExcelUtil<CarList> util = new ExcelUtil<CarList>(CarList.class);
        util.exportExcel(response, list, "汽车数据");
    }

    /**
     * 获取汽车详细信息
     */
    @PreAuthorize("@ss.hasPermi('car:list:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId) {
        return success(carListService.selectCarListByCarId(carId));
    }

    /**
     * 新增汽车
     */
    @PreAuthorize("@ss.hasPermi('car:list:add')")
    @Log(title = "汽车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarList carList) {
        return toAjax(carListService.insertCarList(carList));
    }

    /**
     * 修改汽车
     */
    @PreAuthorize("@ss.hasPermi('car:list:edit')")
    @Log(title = "汽车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarList carList) {
        return toAjax(carListService.updateCarList(carList));
    }

    /**
     * 删除汽车
     */
    @PreAuthorize("@ss.hasPermi('car:list:remove')")
    @Log(title = "汽车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds) {
        return toAjax(carListService.deleteCarListByCarIds(carIds));
    }
}